extends Node2D
@onready var player: Node2D = $player
@onready var polygon_alpha: Polygon2D = $Polygon_alpha

var dragging:bool #拖拽状态
var v2_mouse:Vector2i #鼠标的偏差

var window_model:int = 1 # 0=window模式  1=alpha模式
var shift_down:bool = false #shift按下、抬起
var val_scal:float = 1 #缩放比例
@onready var v_slider: VSlider = $Control/VSlider


@onready var v2_p:PackedVector2Array


#退出程序
func _unhandled_input(event):
	if event.is_action_pressed("exit"):
		get_tree().quit()
	#窗口模式
	if  event.is_action("shift") and shift_down == false and window_model==1:
		DisplayServer.window_set_mouse_passthrough([])
		window_model = 0
		v_slider.visible = true
		shift_down = true
	#alpha模式
	if  event.is_action("shift") and shift_down == false and window_model==0:
		alpha_screen()
		window_model = 1
		v_slider.visible = false
		shift_down = true
	#shift抬起
	if event.is_action_released("shift"):
		shift_down = false
	

func alpha_screen():
	v2_p.resize(polygon_alpha.polygon.size())
	for i in v2_p.size():
		#v2_p[i]=polygon_alpha.polygon[i]+player.position
		v2_p[i]=polygon_alpha.polygon[i]*val_scal
	DisplayServer.window_set_mouse_passthrough(v2_p)

#修改窗体可点击区域
#func _physics_process(_delta) -> void:
func _ready() -> void:
	v_slider.visible = false
	alpha_screen()
		
#拖拽窗口
func _input(event):
	if event is InputEventMouseButton and window_model == 1:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			dragging = true
			v2_mouse =get_global_mouse_position()
		else:
			dragging = false
			
	if event is InputEventMouseMotion and window_model == 1 and dragging:
		#窗口位置=鼠标位置  - 鼠标的偏差值
		DisplayServer.window_set_position(DisplayServer.mouse_get_position()-v2_mouse)


func _on_v_slider_value_changed(value: float) -> void:
	val_scal = v_slider.value
	player.scale.x=val_scal
	player.scale.y=val_scal
